package com.tool.cn.util;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.tool.cn.model.HtmlFile;

/**
 * Jsoup爬虫工具类
 * 
 * @author wangzh 2018年1月2日 下午3:35:35
 */
public class JsoupUtil {

	private static final Log Log = LogFactory.getLog(JsoupUtil.class);

	/**
	 * 获取数据路径页面信息
	 * 
	 * @return
	 */
	public static Document getHtml(String url) {
		Document doc = null;
		try {
			doc = Jsoup.connect(url).timeout(5 * 60000).get();
		} catch (Exception e) {
			Log.info("获取" + url + "数据：" + e);
		}
		return doc;
	}

	/**
	 * 去除标签中的 id和class
	 */
	public String removeLable(Element element) {
		if (element == null) {
			return "";
		}
		element.removeAttr("class").removeAttr("id");
		element.select("p").removeAttr("class");

		element.select("script").remove();

		// 移除注释
		Pattern p = Pattern.compile("\\<!--(.+)--\\>");
		Matcher m = p.matcher(element.toString());
		return m.replaceAll("");
	}

	/**
	 * 复制文件
	 */
	public static boolean makeHtmlLinkFile(Document doc, String filepath, int timeout) {
		
//		File file = new File(filepath);
//		if (file.exists()) {
//			try {
//				FileUtils.cleanDirectory(new File(filepath));
//			} catch (IOException e) {
//				System.out.println("清空文件夹 :" + filepath + " 错误");
//			}
//		
//		}
		List<HtmlFile> list = getHtmlFileLink(doc);
		for (HtmlFile htmlFile : list) {
			try {
				if ((htmlFile.getType().equals("jpg")) || (htmlFile.getType().equals("png"))
						|| (htmlFile.getType().equals("gif")) || (htmlFile.getType().equals("cn"))) {
					String filepaths = filepath + "\\images\\";
					FileUtils.copyURLToFile(new URL(htmlFile.getHref()), new File(filepaths + htmlFile.getFilename()),
							timeout, timeout);
					Log.info("保存图片" + htmlFile.getHref());
				}

			} catch (MalformedURLException e) {
				Log.info("地址 :" + htmlFile.getHref() + " 链接错误");
				return false;
			} catch (IOException e) {
				Log.info("地址 :" + htmlFile.getHref() + " 链接超时");
				return false;
			}
		}
		return true;
	}

	/**
	 * 文件路径重命名
	 * 
	 * @param doc
	 */
	public static void renameHref(Document doc) {
		List<HtmlFile> list = getHtmlFileLink(doc);
		if (list.size() == 0) {
			return;
		}

		Elements imports = doc.select("link[href]");
		for (Element link : imports) {
			for (HtmlFile htmlFileLink : list) {
				if (link.attr("abs:href").equals(htmlFileLink.getHref())) {
					link.attr("href", "../css/" + htmlFileLink.getFilename());
				}
			}

		}

		Elements src = doc.select("[src]");
		for (Element link : src)
			if (link.tagName().equals("script")) {
				for (HtmlFile htmlFileLink : list) {
					if (link.attr("abs:src").equals(htmlFileLink.getHref())) {
						link.attr("src", "../js/" + htmlFileLink.getFilename());
					}
				}
			} else {
				for (HtmlFile htmlFileLink : list)
					if (link.attr("abs:src").equals(htmlFileLink.getHref()))
						link.attr("src", "images/" + htmlFileLink.getFilename());
			}
	}

	/**
	 * 获取页面资源
	 * 
	 * @param doc
	 * @return
	 */
	public static List<HtmlFile> getHtmlFileLink(Document doc) {
		List<HtmlFile> HtmlFileLinks = new ArrayList<HtmlFile>();
		String postfix = "";
		int index = 0;
		String href;

		Elements html = doc.select("a[href]");
		for (Element link : html) {
			href = link.attr("abs:href");
			postfix = getPostfix(href);
			String filename = postfix + index + "." + postfix;
			HtmlFileLinks.add(new HtmlFile(href, filename, postfix));
			index++;
		}

		// css 样式
		Elements importcss = doc.select("link[href]");

		for (Element link : importcss) {

			if (link.attr("rel").equals("stylesheet")) {
				href = link.attr("abs:href");
				postfix = getPostfix(href);
				String filename = postfix + index + "." + postfix;
				HtmlFileLinks.add(new HtmlFile(href, filename, postfix));
				index++;
			}

		}

		String src;
		// 图片等
		Elements media = doc.select("[src]");
		for (Element link : media) {
			if (link.tagName().equals("img")) {
				src = link.attr("abs:src");
				postfix = getPostfix(src);
				String filename = postfix + index + "." + postfix;
				HtmlFileLinks.add(new HtmlFile(src, filename, postfix));
				index++;
			}
			if ((link.tagName().equals("input")) && (link.attr("type").equals("image"))) {
				src = link.attr("abs:src");
				postfix = getPostfix(src);
				String filename = postfix + index + "." + postfix;
				HtmlFileLinks.add(new HtmlFile(src, filename, postfix));
				index++;
			}

			if ((link.tagName().equals("javascript")) || (link.tagName().equals("script"))) {
				src = link.attr("abs:src");
				postfix = getPostfix(src);
				String filename = postfix + index + "." + postfix;
				HtmlFileLinks.add(new HtmlFile(src, filename, postfix));
				index++;
			}
			if (link.tagName().equals("iframe")) {
				// 真实路径
				src = link.attr("abs:src");
				postfix = getPostfix(src);
				String filename = postfix + index + ".html";
				HtmlFileLinks.add(new HtmlFile(src, filename, postfix));
				index++;
			}
			if (link.tagName().equals("embed")) {
				src = link.attr("abs:src");
				postfix = getPostfix(src);
				String filename = postfix + index + "." + postfix;
				HtmlFileLinks.add(new HtmlFile(src, filename, postfix));
				index++;
			}
		}
		return HtmlFileLinks;
	}

	public static String getPostfix(String filename) {
		filename = StringUtils.substringAfterLast(filename, ".");
		filename = StringUtils.substringBefore(filename, "?");
		filename = StringUtils.substringBefore(filename, "/");
		filename = StringUtils.substringBefore(filename, "\\");
		filename = StringUtils.substringBefore(filename, "&");
		filename = StringUtils.substringBefore(filename, "$");
		filename = StringUtils.substringBefore(filename, "%");
		filename = StringUtils.substringBefore(filename, "#");
		filename = StringUtils.substringBefore(filename, "@");
		return filename;
	}

	/**
	 * 去除点击事件
	 * 
	 * @param doc
	 */
	public static void removeHref(Document doc) {
		doc.select("img").removeAttr("onclick");
		doc.select("input[type=submit]").attr("type", "button");
		doc.select("input[type=button]").removeAttr("onclick");
		doc.select("area").removeAttr("href");
	}

	/**
	 * 生成文件
	 */
	public static boolean makeHtmlByDoc(String filepath, String filename, Document doc, String ecoding) {
		boolean flag = false;
		try {
			String html = doc.html();
			html = html.replaceAll("\n", "\r\n");
			FileUtils.writeStringToFile(new File(filepath + filename), html, ecoding);
			flag = true;
		} catch (IOException e) {
			System.out.println("生成页面错误 文件： " + filepath + filename);
		}
		System.out.println("生成html页面 文件 : " + filepath + filename + "完成");
		return flag;
	}
}
